/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.zwh.autoconfigure.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.zwh.autoconfigure.interceptor.LoginInterceptor;
import com.zwh.autoconfigure.interceptor.WebInterceptorProperties;
import com.zwh.common.lang.StringUtils;

/**
 * 是否登录拦截器
 *
 * @author ThinkGem
 * @version 2018年1月10日
 */
@Configuration
@ConditionalOnProperty(name = "web.interceptor.login.enabled", havingValue = "true", matchIfMissing = true)
public class LoginInterceptorConfig implements WebMvcConfigurer {

    @Autowired
    WebInterceptorProperties webInterceptorProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
        List<String> addPathPatterns = webInterceptorProperties.getLogin().getAddPathPatterns();
        List<String> excludePathPatterns = webInterceptorProperties.getLogin().getExcludePathPatterns();
        for (String uri : addPathPatterns) {
            if (StringUtils.isNotBlank(uri)) {
                registration.addPathPatterns(StringUtils.trim(uri));
            }
        }
        for (String uri : excludePathPatterns) {
            if (StringUtils.isNotBlank(uri)) {
                registration.excludePathPatterns(StringUtils.trim(uri));
            }
        }
    }

}